/**
 * 添加或者修改页面
 */
var OrderPutInfoDlg = {
    data: {
        putNumber: "",
        putCustomerId: "",
        putCustomerName: "",
        putCustomerNumber: "",
        estimatetime: "",
        warehouseId: "",
        warehouseName: "",
        putType: "",
        origin: "",
        sourceunit: "",
        settlement: "",
        carnumber: "",
        shipmentnumber: "",
        billstate: "",
        notes: "",
        version: "",
        deliverId: "",
        createTime: "",
        updateTime: "",
        submissionTime: "",
        examineTime: "",
        startTime: "",
        completeTime: "",
        createUser: "",
        updateUser: "",
        submissionUser: "",
        examineUser: "",
        startUser: "",
        completeUser: "",
        createName: "",
        updateName: "",
        submissionName: "",
        examineName: "",
        startName: "",
        completeName: ""
    }
};
/**
 * 添加或者修改页面
 */
var OrderTransportInfoDlg = {
    data: {
    	setUnitId: "",
        setUnit: "",
        setLocation: "",
        setPeople: "",
        setTel: "",
        setMentiontime: "",
        setWarehousepeople: "",
        setWarehousetel: "",
        setAddress: "",
        getUnitId: "",
        getUnit: "",
        getLocation: "",
        getPeople: "",
        getTel: "",
        getMentiontime: "",
        getWarehousepeople: "",
        getWarehousetel: "",
        getAddress: "",
    }
};
/**
 * 添加或者修改页面
 */
var AddressInfoDlg = {
    data: {
    	setLocation: "",
        setLocationId: "",
    	setPeople: "",
    	setTel: "",
    }
};
var submit = 0
layui.use(['form', 'admin', 'ax','laydate'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //执行一个laydate实例
    laydate.render({
      elem: '#estimatetime' //指定元素
    });

    //执行一个laydate实例
    laydate.render({
      elem: '#setMentiontime' //指定元素
    });

    //执行一个laydate实例
    laydate.render({
      elem: '#getMentiontime' //指定元素
    });

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/orderPut/addItem", function (data) {
        	if (data.success) {
                Feng.success("Add successful!");
                window.location.href = Feng.ctxPath + '/orderPut'
		} else {
                Feng.error("Add failed! " + data.message);
		}
        }, function (data) {
            Feng.error("Add failed! " + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });
    
    //Select inbound method
    layui.use(['form'], function() {
        var form=layui.form;
	    form.on('select(putMode)', function(data){
	    	var val = data.value;
	    	if (val=='Pickup at Location') {
	    		$("#unit-h").show();
	    		$("#verify").val("2");
			} else {
	    		$("#unit-h").hide();
	    		$("#verify").val("1");
	    	}
		});
    });
    
    $(document).ready(function(){ 
		$("#unit-h").hide();
		$("#verify").val("1");
    });   
    
    //点击装货单位
    $("#setUnit").click(function(){
		submit = 0
        layer.open({
            type: 2,
            title: 'Select Loading Unit',
            area: ['900px', '600px'],
            content: Feng.ctxPath + '/orderTransport/toChoiceUnit?putgoodsCustomerId='+$("#putCustomerId").val(),
            end: function () {
            	if(submit == 1){
	                $("#setUnitId").val(OrderTransportInfoDlg.data.setUnitId);
	                $("#setUnit").val(OrderTransportInfoDlg.data.setUnit);
	                $("#setLocation").val(OrderTransportInfoDlg.data.setLocation);
	                $("#setLocationId").val(OrderTransportInfoDlg.data.setLocationId);
	                $("#setPeople").val(OrderTransportInfoDlg.data.setPeople);
	                $("#setTel").val(OrderTransportInfoDlg.data.setTel);
            	}
            }
        });
    });
    
    //点击卸货单位
    $("#getUnit").click(function(){
		submit = 0
        layer.open({
            type: 2,
            title: 'Select Unloading Unit',
            area: ['900px', '600px'],
            content: Feng.ctxPath + '/orderTransport/toChoiceUnit?putgoodsCustomerId='+$("#putCustomerId").val(),
            end: function () {
            	if(submit == 1){
	                $("#getUnitId").val(OrderTransportInfoDlg.data.setUnitId);
	                $("#getUnit").val(OrderTransportInfoDlg.data.setUnit);
	                $("#getLocation").val(OrderTransportInfoDlg.data.setLocation);
	                $("#getLocationId").val(OrderTransportInfoDlg.data.setLocationId);
	                $("#getPeople").val(OrderTransportInfoDlg.data.setPeople);
	                $("#getTel").val(OrderTransportInfoDlg.data.setTel);
            	}
            }
        });
    });
    
    //点击 装货地址
    $("#setLocation").click(function(){
    	var setUnit = $("#setUnit").val();
    	if (setUnit==null||setUnit=='') {
			layer.msg('Please select loading unit'); 
		} else {
			submit = 0
            layer.open({
                type: 2,
                title: 'Select Loading Address',
                area: ['900px', '600px'],
                content: Feng.ctxPath + '/orderTransport/toAddress?putgoodsId=' + $("#setUnitId").val(),
                end: function () {
            		if(submit == 1){
	                    $("#setLocation").val(AddressInfoDlg.data.setLocation);
	                    $("#setLocationId").val(AddressInfoDlg.data.setLocationId);
	                    $("#setPeople").val(AddressInfoDlg.data.setPeople);
	                    $("#setTel").val(AddressInfoDlg.data.setTel);
            		}
                }
            });
		}
    });
    
    //点击 卸货地址
    $("#getLocation").click(function(){
    	var getUnit = $("#getUnit").val();
    	if (getUnit==null||getUnit=='') {
			layer.msg('Please select unloading unit'); 
		} else {
			submit = 0
            layer.open({
                type: 2,
                title: 'Select Unloading Address',
                area: ['900px', '600px'],
                content: Feng.ctxPath + '/orderTransport/toAddress?putgoodsId=' + $("#getUnitId").val(),
                end: function () {
            		if(submit == 1){
	                    $("#getLocation").val(AddressInfoDlg.data.setLocation);
	                    $("#getLocationId").val(AddressInfoDlg.data.setLocationId);
	                    $("#getPeople").val(AddressInfoDlg.data.setPeople);
	                    $("#getTel").val(AddressInfoDlg.data.setTel);
            		}
                }
            });
		}
    });

    //点击 选择位置
    $("#choicep").click(function(){
        var getLocation = $("#getLocation").val();
        if (getLocation==null||getLocation=='') {
            layer.msg('Please select unloading address');
        } else {
			submit = 0
            layer.open({
                type: 2,
                title: 'Edit Unloading Address',
                area: ['1000px', '800px'],
                content: Feng.ctxPath + '/outOrder/editAddress?id=' + $("#getLocationId").val(),
                end: function () {
            		if(submit == 1){
	                    $("#getLocation").val(AddressInfoDlg.data.setLocation);
					}
                }
            });
        }
    });
    
    form.verify({
    	setUnit: function(value, item){ //value：form value、item：form DOM object
    		if($("#verify").val()=='2'){
    			if(value==null||value==''){
					console.log(1)
    				return 'Please select loading unit';
    			}
    		}
    	 },
    	setMentiontime: function(value, item){ //value：form value、item：form DOM object
    		if($("#verify").val()=='2'){
    		    if(value==null||value==''){
					console.log(1)
    		    	return 'Please select pickup time';
    		    }
    		}
	 },
    	 getUnit: function(value, item){ //value：form value、item：form DOM object
    		if($("#verify").val()=='2'){
    		    if(value==null||value==''){
					console.log(1)
    		    	return 'Please select unloading unit';
    		    }
    		}
	 },
	 setLocation: function(value, item){ //value：form value、item：form DOM object
    		if($("#verify").val()=='2'){
     		    if(value==null||value==''){
					console.log(1)
     		    	return 'Please select loading address';
     		    }
    		}
 	},
	 getLocation: function(value, item){ //value：form value、item：form DOM object
    		if($("#verify").val()=='2'){
	 		    if(value==null||value==''){
					console.log(1)
	 		    	return 'Please select unloading address';
	 		    }
    		}
 	}
    });

    //Back button
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/orderPut'
    });

});